/**
 * Copyright (C) 2010 David Simonek <dafesimonek@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DbLocationConfig.java
 *
 * Created on Oct 12, 2010, 11:47:08 AM
 */

package org.dafe.partyband.gui;

import org.dafe.partyband.db.api.DbManager;
import java.awt.Color;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.openide.DialogDescriptor;
import org.openide.util.NbBundle;

/**
 *
 * @author dafe
 */
public class DbLocationClientConfig extends javax.swing.JPanel {
    private DialogDescriptor dd;
    private Color origDirFg;

    /** Creates new form DbLocationConfig */
    public DbLocationClientConfig() {
        initComponents();

        localRadio.setSelected(DbManager.LocationType.LOCAL.equals(DbManager.getLocationType()));
        dirField.setText(DbManager.getLocalDir());

        dirField.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                checkValidity();
            }

            public void removeUpdate(DocumentEvent e) {
                checkValidity();
            }

            public void changedUpdate(DocumentEvent e) {
                checkValidity();
            }
        });
    }

    @Override
    public void addNotify() {
        super.addNotify();
        origDirFg = dirField.getForeground();
    }

    public String getLocalDir () {
        return dirField.getText();
    }

    public void attachDescriptor (DialogDescriptor dd) {
        this.dd = dd;
        checkValidity();
    }

    private void checkValidity () {
        assert dd != null : "Dialog descriptor must be attached";
        File dir = new File(dirField.getText());
        boolean isValid = dir.exists() && dir.isDirectory();
        dd.setValid(isValid);
        if (isValid) {
            dd.getNotificationLineSupport().clearMessages();
            dirField.setForeground(origDirFg != null ? origDirFg : new JTextField().getForeground());
        } else {
            dd.getNotificationLineSupport().setErrorMessage(
                    NbBundle.getMessage(DbLocationClientConfig.class, "MSG_DbDirNotValid"));
            dirField.setForeground(Color.red);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        localRadio = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        dirField = new javax.swing.JTextField();
        chooseDirButton = new javax.swing.JButton();
        remoteRadio = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        hostField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        portField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        userField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        passwordField = new javax.swing.JTextField();

        localRadio.setSelected(true);
        localRadio.setText(org.openide.util.NbBundle.getMessage(DbLocationClientConfig.class, "DbLocationClientConfig.localRadio.text",new Object[] {})); // NOI18N

        jLabel1.setLabelFor(dirField);
        jLabel1.setText(org.openide.util.NbBundle.getMessage(DbLocationClientConfig.class, "DbLocationClientConfig.jLabel1.text",new Object[] {})); // NOI18N

        dirField.setText(org.openide.util.NbBundle.getMessage(DbLocationClientConfig.class, "DbLocationClientConfig.dirField.text",new Object[] {})); // NOI18N
        dirField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dirFieldActionPerformed(evt);
            }
        });

        chooseDirButton.setText(org.openide.util.NbBundle.getMessage(DbLocationClientConfig.class, "DbLocationClientConfig.chooseDirButton.text",new Object[] {})); // NOI18N
        chooseDirButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseDirButtonActionPerformed(evt);
            }
        });

        remoteRadio.setText(org.openide.util.NbBundle.getMessage(DbLocationClientConfig.class, "DbLocationClientConfig.remoteRadio.text",new Object[] {})); // NOI18N
        remoteRadio.setEnabled(false);

        jLabel2.setLabelFor(hostField);
        jLabel2.setText(org.openide.util.NbBundle.getMessage(DbLocationClientConfig.class, "DbLocationClientConfig.jLabel2.text",new Object[] {})); // NOI18N
        jLabel2.setEnabled(false);

        hostField.setText(org.openide.util.NbBundle.getMessage(DbLocationClientConfig.class, "DbLocationClientConfig.hostField.text",new Object[] {})); // NOI18N
        hostField.setEnabled(false);
        hostField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hostFieldActionPerformed(evt);
            }
        });

        jLabel3.setLabelFor(portField);
        jLabel3.setText(org.openide.util.NbBundle.getMessage(DbLocationClientConfig.class, "DbLocationClientConfig.jLabel3.text",new Object[] {})); // NOI18N
        jLabel3.setEnabled(false);

        portField.setText(org.openide.util.NbBundle.getMessage(DbLocationClientConfig.class, "DbLocationClientConfig.portField.text",new Object[] {})); // NOI18N
        portField.setEnabled(false);
        portField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                portFieldActionPerformed(evt);
            }
        });

        jLabel4.setLabelFor(userField);
        jLabel4.setText(org.openide.util.NbBundle.getMessage(DbLocationClientConfig.class, "DbLocationClientConfig.jLabel4.text",new Object[] {})); // NOI18N
        jLabel4.setEnabled(false);

        userField.setText(org.openide.util.NbBundle.getMessage(DbLocationClientConfig.class, "DbLocationClientConfig.userField.text",new Object[] {})); // NOI18N
        userField.setEnabled(false);

        jLabel5.setLabelFor(passwordField);
        jLabel5.setText(org.openide.util.NbBundle.getMessage(DbLocationClientConfig.class, "DbLocationClientConfig.jLabel5.text",new Object[] {})); // NOI18N
        jLabel5.setEnabled(false);

        passwordField.setText(org.openide.util.NbBundle.getMessage(DbLocationClientConfig.class, "DbLocationClientConfig.passwordField.text",new Object[] {})); // NOI18N
        passwordField.setEnabled(false);
        passwordField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                passwordFieldActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dirField, javax.swing.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(chooseDirButton))
                    .addComponent(localRadio)
                    .addComponent(remoteRadio)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel2)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(hostField, javax.swing.GroupLayout.DEFAULT_SIZE, 237, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(portField, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(passwordField, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(userField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(localRadio)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(dirField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(chooseDirButton))
                .addGap(18, 18, 18)
                .addComponent(remoteRadio)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(hostField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(portField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(userField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void dirFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dirFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_dirFieldActionPerformed

    private void hostFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hostFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_hostFieldActionPerformed

    private void portFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_portFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_portFieldActionPerformed

    private void passwordFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_passwordFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_passwordFieldActionPerformed

    private void chooseDirButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseDirButtonActionPerformed
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fileChooser.setDialogTitle(NbBundle.getMessage(DbLocationClientConfig.class,
                "LBL_ChooseClientDbDir"));
        int result = fileChooser.showDialog(SwingUtilities.getWindowAncestor(this),
                NbBundle.getMessage(DbLocationClientConfig.class, "LBL_PerformChooseDbDir"));
        if (result == JFileChooser.APPROVE_OPTION) {
            dirField.setText(fileChooser.getSelectedFile().getPath());
        }

    }//GEN-LAST:event_chooseDirButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton chooseDirButton;
    private javax.swing.JTextField dirField;
    private javax.swing.JTextField hostField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JRadioButton localRadio;
    private javax.swing.JTextField passwordField;
    private javax.swing.JTextField portField;
    private javax.swing.JRadioButton remoteRadio;
    private javax.swing.JTextField userField;
    // End of variables declaration//GEN-END:variables

}
